'''
    Goblinoid: Experience all of MediaGoblin on an Android Device
    Copyright (C) 2015 Dylan Jeffers

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
'''

from __future__ import print_function

from kivy import utils
from kivy.lang import Builder
from kivy.animation import Animation
from kivy.properties import ListProperty, ObjectProperty
from goblinoid.root.goblinrecycleview import GoblinRecycleView
from goblinoid.root.goblinscreen import GoblinScreen, GoblinScreenWithManager

Builder.load_file('goblinoid/feed/kv/feedview.kv')


class FeedScreen(GoblinScreen):

    feed_service = ObjectProperty()
    rv_height = ObjectProperty()

    goblin_bar_animation = ObjectProperty()
    feed_bar_animation = ObjectProperty()
    max_color = ListProperty(utils.get_color_from_hex('#86D4B1'))
    min_color = ListProperty(utils.get_color_from_hex("#2a4237"))
    base_color = ListProperty([0, 0, 0, .1])
    error_color = ListProperty(utils.get_color_from_hex("#ef4832"))

    def __init__(self, **kwargs):
        super(FeedScreen, self).__init__(**kwargs)
        self.rv_old_height = kwargs['dynamic_props']['rv_height']

    def on_enter(self):
        if len(self.feed_service.activity_list) > 0:
            self.ids.rv.old_height = self.rv_old_height
            self.ids.rv.update_feed_data(self.feed_service.update_feed_data())
        else:
            self.on_pull_to_refresh()

    def on_overscroll(self, pos):
        percent_from_full_pull = pos
        max_base_color_diff = [self.max_color[i] - self.base_color[i] for i in range(len(self.max_color))]
        added_color = [max_base_color_diff[i] * percent_from_full_pull for i in range(len(max_base_color_diff))]
        self.ids.feed_bar.color = [self.base_color[i] +
                                   added_color[i] for i in range(
                                       len(added_color))]

    def on_pull_to_refresh(self):
        self.feed_bar_animation = Animation(
            color=self.max_color,
            duration=1) + Animation(
                color=self.min_color,
                duration=1)
        self.feed_bar_animation.repeat = True
        self.feed_bar_animation.start(self.ids.feed_bar)
        self.ids.rv.request_feed_refresh()

    def on_feed_refreshed(self, *largs):
        if self.feed_bar_animation is not None:
            self.feed_bar_animation.repeat = False
            self.feed_bar_animation.stop_all(self.ids.feed_bar)
        end_anim = Animation(color=self.base_color)
        end_anim.start(self.ids.feed_bar)

    def on_no_service(self, *largs):
        self.feed_bar_animation.repeat = False
        self.feed_bar_animation.stop_all(self.ids.feed_bar)
        error_anim = Animation(color=self.error_color)
        error_anim.start(self.ids.feed_bar)


class RecycleFeed(GoblinRecycleView):
    pass


class FeedView(GoblinScreenWithManager):

    def __init__(self, **kwargs):
        super(FeedView, self).__init__(**kwargs)
        self.ids.sm.open_screen(class_name='feed_rv', name='feed_rv')
